package com.device.entity;

import javax.persistence.*;
import java.util.UUID;

/**
 * Modbus配置信息
 */
@Entity
@Table(name = "device_keys")
public class DeviceKey {
    
    @Id
    @Column(name = "id")
    private UUID id;
    
    @Column(name = "sub_device_id", nullable = false)
    private UUID subDeviceId;           // 关联分设备ID
    
    @Column(name = "key_number", nullable = false)
    private Integer keyNumber;          // 编号
    
    @Column(name = "key_name", nullable = false)
    private String keyName;             // 上报key名称
    
    @Column(name = "slave_address", nullable = false)
    private Integer slaveAddress;       // 从站地址
    
    @Column(name = "operation_type", nullable = false)
    private Integer operationType;      // 读和写 (1:读, 2:写)
    
    @Column(name = "function_code", nullable = false)
    private Integer functionCode;       // 功能码 (1-8对应不同操作)
    
    @Column(name = "function_value", nullable = false)
    private Integer functionValue;      // 功能对应的2位数值
    
    @Column(name = "register_start_address", nullable = false)
    private Integer registerStartAddress; // 寄存器起始地址
    
    @Column(name = "register_count", nullable = false)
    private Integer registerCount;      // 寄存器读取数量
    
    @Column(name = "byte_order", nullable = false)
    private Integer byteOrder;          // 字节顺序 (1:大端, 2:小端, 3:中端, 4:混合端)
    
    @Column(name = "multiplier", nullable = false)
    private int multiplier;         // 倍率，支持负数
    
    @Column(name = "created_time")
    private Long createdTime;
    
    @Column(name = "updated_time")
    private Long updatedTime;
    
    // Constructors
    public DeviceKey() {}
    
    public DeviceKey(UUID subDeviceId, Integer keyNumber, String keyName, Integer slaveAddress,
                    Integer operationType, Integer functionCode, Integer functionValue,
                    Integer registerStartAddress, Integer registerCount, Integer byteOrder, int multiplier) {
        this.id = UUID.randomUUID();
        this.subDeviceId = subDeviceId;
        this.keyNumber = keyNumber;
        this.keyName = keyName;
        this.slaveAddress = slaveAddress;
        this.operationType = operationType;
        this.functionCode = functionCode;
        this.functionValue = functionValue;
        this.registerStartAddress = registerStartAddress;
        this.registerCount = registerCount;
        this.byteOrder = byteOrder;
        this.multiplier = multiplier != 0 ? multiplier : 1;
        this.createdTime = System.currentTimeMillis();
        this.updatedTime = System.currentTimeMillis();
    }

    // Getters and Setters
    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public UUID getSubDeviceId() {
        return subDeviceId;
    }

    public void setSubDeviceId(UUID subDeviceId) {
        this.subDeviceId = subDeviceId;
    }

    public Integer getKeyNumber() {
        return keyNumber;
    }

    public void setKeyNumber(Integer keyNumber) {
        this.keyNumber = keyNumber;
    }

    public String getKeyName() {
        return keyName;
    }

    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }

    public Integer getSlaveAddress() {
        return slaveAddress;
    }

    public void setSlaveAddress(Integer slaveAddress) {
        this.slaveAddress = slaveAddress;
    }

    public Integer getOperationType() {
        return operationType;
    }

    public void setOperationType(Integer operationType) {
        this.operationType = operationType;
    }

    public Integer getFunctionCode() {
        return functionCode;
    }

    public void setFunctionCode(Integer functionCode) {
        this.functionCode = functionCode;
    }

    public Integer getFunctionValue() {
        return functionValue;
    }

    public void setFunctionValue(Integer functionValue) {
        this.functionValue = functionValue;
    }

    public Integer getRegisterStartAddress() {
        return registerStartAddress;
    }

    public void setRegisterStartAddress(Integer registerStartAddress) {
        this.registerStartAddress = registerStartAddress;
    }

    public Integer getRegisterCount() {
        return registerCount;
    }

    public void setRegisterCount(Integer registerCount) {
        this.registerCount = registerCount;
    }

    public Integer getByteOrder() {
        return byteOrder;
    }

    public void setByteOrder(Integer byteOrder) {
        this.byteOrder = byteOrder;
    }

    public int getMultiplier() {
        return multiplier;
    }

    public void setMultiplier(int multiplier) {
        this.multiplier = multiplier;
    }

    public Long getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Long createdTime) {
        this.createdTime = createdTime;
    }

    public Long getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(Long updatedTime) {
        this.updatedTime = updatedTime;
    }

    @Override
    public String toString() {
        return "DeviceKey{" +
                "id=" + id +
                ", subDeviceId=" + subDeviceId +
                ", keyNumber=" + keyNumber +
                ", keyName='" + keyName + '\'' +
                ", slaveAddress=" + slaveAddress +
                ", operationType=" + operationType +
                ", functionCode=" + functionCode +
                ", functionValue=" + functionValue +
                ", registerStartAddress=" + registerStartAddress +
                ", registerCount=" + registerCount +
                ", byteOrder=" + byteOrder +
                ", multiplier=" + multiplier +
                ", createdTime=" + createdTime +
                ", updatedTime=" + updatedTime +
                '}';
    }
}